package com.shuangyulin.domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.shuangyulin.dao.PidDAO;

public class TxtFile extends ConfigFile{

	private File file;
	
	
	public TxtFile(File cfgFile){
		this.file = cfgFile;
	}
	
	public Pid stringToPid(String line){
		HashMap<String, String> mapPid =new  HashMap<String, String>();	
		Pid piditem = new Pid();
		if(line.matches("^#index:pid.*")){
			pidNameList=line.split(":[ ]*");
			System.out.println("pidNameList: " + pidNameList.length);
			piditem.setPidIndex(0);
			return piditem;
			
		} else{
			
			String pidItemList[] =line.split(":[ ]*");
			//System.out.println("pidItem: " + pidItemList[0] +pidItemList[1] +pidItemList[2] +pidItemList[3] +pidItemList[4]);
			//System.out.println("pidItemList: " + pidItemList.length);
			
			if (pidNameList.length != pidItemList.length)
			{
				System.out.println("idd config file error");
				return null;
				
			} else{
				for(int i=0;i<pidNameList.length;i++) {					
					mapPid.put(pidNameList[i], pidItemList[i]);
				}
				
			}
			
			//System.out.println(pidNameList[0]+":"+ mapPid.get(pidNameList[0]));
			//System.out.println("1test:"+ Integer.parseInt(mapPid.get("#index")));
			piditem.setPidIndex(Integer.parseInt(mapPid.get("#index")));  //1
			//System.out.println("setPidIndex:"+ piditem.getPidIndex());
			
			piditem.setPidName(mapPid.get("pidname")); //2
			piditem.setSize(Integer.parseInt(mapPid.get("size"))); //3
			piditem.setPidType(Integer.parseInt(mapPid.get("pidtype"))); //4
			piditem.setType(Integer.parseInt(mapPid.get("type"))); //5
			piditem.setAlarmStatus(Integer.parseInt(mapPid.get("AlarmStatus"))); //6
			piditem.setBypassStatus(Integer.parseInt(mapPid.get("BypassStatus"))); //7
			piditem.setCompState(Integer.parseInt(mapPid.get("CompState"))); //8
			piditem.setOpvarType(Integer.parseInt(mapPid.get("OpvarType"))); //9
			piditem.setInhibited(Integer.parseInt(mapPid.get("Inhibited"))); //10
			piditem.setAck(Integer.parseInt(mapPid.get("ACK"))); //11
			piditem.setFlag(Integer.parseInt(mapPid.get("Flag"))); //12
			piditem.setChannelType(Integer.parseInt(mapPid.get("ChannelType"))); //13
			piditem.setDescription(mapPid.get("description")); //14

		}
		
		//System.out.println("1111pidItem: " + piditem);
		
		return piditem;
	}

	public int readCfgFile(){
		BufferedReader reader = null;
		FileReader fileReader = null;
		String tempString = null;
		Pid pidItem = null;

		try {
			fileReader=new FileReader(file);
			reader = new BufferedReader(fileReader);
			try {
				while((tempString = reader.readLine()) != null){
					//System.out.println(tempString);
					pidItem = stringToPid(tempString);
					
					
					if (pidItem != null)
					{
						//System.out.println("pidItem:" + pidItem.getPidIndex());
						//Integer y = new Integer(pidItem.getPidIndex());
						pidList.put(pidItem.getPidIndex(), pidItem);


						//System.out.println("pidItem:: " + pidItem.getPidIndex() + " " +pidItem.getPidName());
					}else{
						System.out.println("pidItem add error");
						continue;
					}
					
					//System.out.println("pidList:"+pidItem.getPidIndex()+" "+ pidList.get(pidItem.getPidIndex()).getPidIndex());
				}
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if(reader!=null)
			{
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				try {
					fileReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
		}
		
		
		return 0;
	}

	public int writeCfgFile() {
		return 0;
	}
}
